﻿Imports IDServer
Imports System.Diagnostics
Imports System.Net.NetworkInformation


Public Class Frm_BSCStoreHouse
    Implements IDServer.IDServer.IDServer


    Private ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp
    Private WithEvents MainFrom As New Frm_MainForm
    Private mDataset As DataSet
    Public bFind As Boolean
    Public Fields As String

    Public stroriginalCountry1, stroriginalProvince1, stroriginalCity1 As String
    Public stroriginalCountry, stroriginalProvince, stroriginalCity As String

    Private Captionstr As String = String.Empty
    Private Captionstr_CHT As String = "倉庫編號^fCode&倉庫簡稱^fShortName&倉庫全稱^fName&存儲貨類^fType&負責人^fEmployeeID&所在城市^fCity&所在省份^fProvince&所在國家^fCountry&倉庫地址^fAddress&聯系電話^fPhone&狀態^fStatus&是否控制到倉位^fStorePlace&備注^fRemark"
    Private Captionstr_CHS As String = "仓库编号^fCode&仓库简称^fShortName&仓库全称^fName&存储货类^fType&负责人^fEmployeeID&所在城市^fCity&所在省份^fProvince&所在国家^fCountry&仓库地址^fAddress&联系电话^fPhone&状态^fStatus&是否控制到仓位^fStorePlace&备注^fRemark"
    Private Captionstr_en As String = "StoreHouse Code^fCode&ShortName^fShortName&Full Name^fName&Storage Category^fType&EmployeeID^fEmployeeID&City^fCity&Province^fProvince&Country^fCountry&Storehouse Adddress^fAddress&Phone^fPhone&Status^fStatus&Control Storehouse^fStorePlace&Remark^fRemark"


    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainFrom
        End Get
        Set(ByVal value As Frm_MainForm)
            MainFrom = value
        End Set
    End Property



#Region "sub or function "


    Private Sub Frm_First_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If mDataset Is Nothing Then
                mDataset = InitializeDataSet()
            End If

            'SetGridCaptionVisible(Me.DataGridView1, Fields, "Frm_BSCCustomer")
            LoadEmployeeID()
            LoadShopName()
            'LoadMultiLanguage()
            SetTextEnable(False)
            With Me.DataGridView1
                .AllowUserToAddRows = False
                .AllowUserToDeleteRows = False
                .MultiSelect = False
                .ReadOnly = True
                .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            End With
            Me.BtnFind_Q.Enabled = FindEnable
            ''变更语言开始语句
            If str_language.ToString.Trim.Length <= 0 Then
                str_language = ReadSystemlanguage()
            End If
            If str_language.ToString.ToString.Length <= 0 Then
                str_language = Msg.Systemdefaultlanguage
            End If
            Select Case str_language
                Case "zh-CHT"
                    str_language = "zh-CHT"
                    Captionstr = Captionstr_CHT
                Case "zh-CHS"
                    str_language = "zh-CHS"
                    Captionstr = Captionstr_CHS
                Case "en"
                    str_language = "en"
                    Captionstr = Captionstr_en
            End Select
            System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(App.str_language)
            DataBindGridView(Me.DataGridView1, Captionstr)
            FormatDataGridView(Me.DataGridView1)
            SetGridCaptionVisible(Me.DataGridView1, Fields, "Frm_BSCCustomer")
            DataBindGrid(Me.DataGridView1, Captionstr)
            BindGridCaption(Me.DataGridView1, BindHashTable(Captionstr))

            With Me.DataGridView1
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
            End With
            ''结束语句
        Catch ex As Exception
            MsgBox("Frm_First_Load:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub
    Private Sub LoadShopName()
        Dim sql As String = ""
        Dim ShopDs As New DataSet
        Try
            sql = "select fCode,fCName from dbo.BSC_Dept where fDefine=1 and fstatus=0"
            ShopDs = ReturnDataset(sql)
            Me.txtfCode.ValueMember = "fCName"
            Me.txtfCode.DisplayMember = "fCode"
            Me.txtfCode.DataSource = ShopDs.Tables(0)
        Catch ex As Exception
            MsgBox("LoadShopName:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Function InitializeDataSet() As DataSet
        Try
            Dim M_Ds As New DataSet
            Dim mTable As New DataTable
            With mTable
                .Columns.Add("fCode")
                .Columns.Add("fShortName")
                .Columns.Add("fName")
                .Columns.Add("fType")
                .Columns.Add("fEmployeeID")
                .Columns.Add("fCity")
                .Columns.Add("fProvince")
                .Columns.Add("fCountry")
                .Columns.Add("fAddress")
                .Columns.Add("fPhone")
                .Columns.Add("fStatus")
                .Columns.Add("fStorePlace")
                .Columns.Add("fRemark")
            End With
            M_Ds.Tables.Add(mTable)
            Return M_Ds
        Catch ex As Exception
            MsgBox("InitializeDataSet;" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
            Return Nothing
        End Try
    End Function

    Private Sub LoadEmployeeID()
        Try
            Dim sql As String = ""
            Dim mTable As New DataTable
            sql = "select distinct fCode,fCName from BSC_Employee"
            mTable = ReturnDataTable(sql)
            If mTable.Rows.Count > 0 Then
                Me.ComEmployeeID.DisplayMember = "fCName"
                Me.ComEmployeeID.ValueMember = "fCode"
                Me.ComEmployeeID.DataSource = mTable
            End If
        Catch ex As Exception
            MsgBox("LoadEmployeeID:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub ActionFind()
        Try
            Dim sql As String = ""
            Dim WhereStr As String = ""
            If Me.fCode.Text.Length > 0 Then
                WhereStr = " and fCode='" & Me.fCode.Text.Replace(",", "").Replace("'", "") & "'"
            End If
            If Me.fCity.Text.Length > 0 Then
                WhereStr += " and fCity='" & Me.fCity.Text.Replace("'", "").Replace(",", "") & "'"
            End If
            If Me.fCountry.Text.Length > 0 Then
                WhereStr += " and fCountry='" & Me.fCountry.Text.Replace("'", "").Replace(",", "") & "'"
            End If
            If Me.fShortName.Text.Length > 0 Then
                WhereStr += " and fShortName='" & Me.fShortName.Text.Replace("'", "").Replace(",", "") & "'"
            End If
            If Me.fType.Text.Length > 0 Then
                WhereStr += "  and fType='" & Me.fType.Text.Replace("'", "").Replace(",", "") & "'"
            End If
            If Me.fStatus.Text.Length > 0 Then

                Select Case Me.fStatus.Text.Trim
                    Case "未禁用"
                        WhereStr += "  and fStatus=0  "
                    Case "已禁用"
                        WhereStr += "  and fStatus=1  "
                    Case Else
                        WhereStr += "  and 1<>1 "
                End Select

            End If

            Me.Cursor = Cursors.WaitCursor
            sql = "select fCode,fShortName,fName,fType,fEmployeeID,fCity,fProvince,fCountry,fAddress,fPhone,fStatus=case fStatus when 0 then '未禁用' when 1 then '已禁用' end,fStorePlace=case fStorePlace when 0 then '否' when 1 then '是' end,fRemark from BSC_StoreHouse where 1=1 " & WhereStr
            mDataset = New DataSet
            mDataset = ReturnDataset(sql)

            Me.DataGridView1.DataSource = mDataset.Tables(0)
            bFind = mDataset.Tables(0).Rows.Count > 0
            'MainFrom.BtnEnable(bFind)
            SetSysEnable(mDataset.Tables(0))
            'GetToolEnable()
            SetButton(ButtonType.Browse, bFind)

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            MessageBox.Show(ex.Message, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub SetTextEnable(ByVal B As Boolean)
        Try
            Me.txtfAddress.Enabled = B
            Me.txtfCity.Enabled = B
            Me.txtfCode.Enabled = B
            Me.txtfCountry.Enabled = B
            Me.txtfName.Enabled = B
            Me.txtfProvince.Enabled = B
            Me.txtfRemark.Enabled = B
            Me.txtfShortName.Enabled = B
            Me.txtfStatus.Enabled = B
            Me.txtfType.Enabled = B
            Me.CBxProhibit.Enabled = B
            Me.CBxControlStoreroom.Enabled = B
            Me.ComEmployeeID.Enabled = B
            Me.BtnOK.Enabled = B
            Me.BtnCancel.Enabled = B
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub ClearText()
        Try
            Me.txtfAddress.Text = ""
            Me.txtfCity.Text = ""
            Me.txtfCode.Text = ""
            Me.txtfCountry.Text = ""
            Me.txtfName.Text = ""
            Me.txtfProvince.Text = ""
            Me.txtfRemark.Text = ""
            Me.txtfShortName.Text = ""
            Me.txtfStatus.Text = ""
            Me.txtfType.Text = ""
            Me.CBxProhibit.Checked = False
            Me.CBxControlStoreroom.Checked = False
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub FillText(ByVal fCode As String)
        Try
            Dim mRow() As DataRow = mDataset.Tables(0).Select("fCode='" & fCode & "'")
            If mRow Is Nothing Then Exit Sub
            For Each Row As DataRow In mRow
                Me.txtfAddress.Text = Row("fAddress") & ""
                Me.txtfCity.Text = Row("fCity") & ""
                Me.txtfCode.Text = Row("fCode") & ""
                Me.txtfCountry.Text = Row("fCountry") & ""
                Me.txtfName.Text = Row("fName") & ""
                Me.txtfProvince.Text = Row("fProvince") & ""
                Me.txtfRemark.Text = Row("fRemark") & ""
                Me.txtfShortName.Text = Row("fShortName") & ""
                Me.txtfStatus.Text = Row("fPhone") & ""
                If Row("fStatus") = "未禁用" Then
                    Me.CBxProhibit.Checked = False
                ElseIf Row("fStatus") = "已禁用" Then
                    Me.CBxProhibit.Checked = True
                End If
                If Row("fStorePlace").ToString = "否" Then
                    Me.CBxControlStoreroom.Checked = False
                ElseIf Row("fStorePlace").ToString = "是" Then
                    Me.CBxControlStoreroom.Checked = True
                End If
                Me.txtfType.Text = Row("fType") & ""
            Next
        Catch ex As Exception
            MsgBox("FillText:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Function BtnActionSave() As Boolean
        Try
            Dim sql As String = ""
            If ActionInitialize() = False Then Exit Function

            Dim txtfCode As String = Me.txtfCode.Text.Replace("'", "").Replace(",", "")
            Dim txtfShortName As String = Me.txtfShortName.Text.Replace("'", "").Replace(",", "")
            Dim txtfName As String = Me.txtfName.Text.Replace("'", "").Replace(",", "")
            Dim txtfType As String = Me.txtfType.Text.Replace("'", "").Replace(",", "")
            Dim txtfEmployeeId As String = Me.ComEmployeeID.SelectedValue
            Dim txtfCity As String = Me.txtfCity.Text.Replace("'", "").Replace(",", "")
            Dim txtfProvince As String = Me.txtfProvince.Text.Replace("'", "").Replace(",", "")
            Dim txtfCountry As String = Me.txtfCountry.Text.Replace("'", "").Replace(",", "")
            Dim txtfAddress As String = Me.txtfAddress.Text.Replace("'", "").Replace(",", "")
            Dim txtfPhone As String = Me.txtfStatus.Text.Replace("'", "").Replace(",", "")
            Dim txtfRemark As String = Me.txtfRemark.Text.Replace("'", "").Replace(",", "")
            Dim fStatus As Integer = IIf(Me.CBxProhibit.Checked, 1, 0)
            Dim fStorePlace As Integer = IIf(Me.CBxControlStoreroom.Checked, 1, 0)

            Dim Fvalue As String = ServerDate
            Dim Fkey1 As String = "period_" & Me.txtfCode.Text.Replace("'", "").Replace(",", "")
            Dim Fkey2 As String = "period_" & Me.txtfCode.Text.Replace("'", "").Replace(",", "") & "_status"
            Dim Fdescription1 As String = "當前倉庫期別_" & Me.txtfShortName.Text.Replace("'", "").Replace(",", "")
            Dim Fdescription2 As String = "Current Stor Period Status"

            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
                sql = "Insert Into BSC_StoreHouse(fCode,fShortName,fName,fType,fEmployeeID,fCity,fProvince,fCountry,fAddress,fPhone,fStatus,fStorePlace,fRemark) values('" & _
                      txtfCode & "','" & txtfShortName & "','" & txtfName & "','" & txtfType & "','" & txtfEmployeeId & "','" & txtfCity & "','" & txtfProvince & _
                      "','" & txtfCountry & "','" & txtfAddress & "','" & txtfPhone & "'," & fStatus & "," & fStorePlace & ",'" & txtfRemark & "');"

                sql += "insert into bsc_systemprofile values('store','" & Fkey1 & "','" & Fvalue & "','" & Fdescription1 & "','','','');"
                sql += "insert into bsc_systemprofile values('store','" & Fkey2 & "','１','" & Fdescription2 & "','','','')"
                ReturnObject(sql)
                AddToDataSet(txtfCode, txtfShortName, txtfName, txtfType, txtfEmployeeId, txtfCity, txtfProvince, txtfCountry, txtfAddress, txtfPhone, fStatus, fStorePlace, txtfRemark)
            ElseIf ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
                sql = "Update BSC_StoreHouse Set fShortName='" & txtfShortName & "',fName='" & txtfName & "',ftype='" & txtfType & "',fEmployeeID='" & txtfEmployeeId & _
                      "',fCity='" & txtfCity & "',fProvince='" & txtfProvince & "',fCountry='" & txtfCountry & "',fAddress='" & txtfAddress & "',fPhone='" & txtfPhone & "',fStatus=" & fStatus & ",fStorePlace=" & fStorePlace & ",fRemark='" & _
                      "' where fCode='" & txtfCode & "'"
                ReturnObject(sql)
                EditToDataSet(txtfCode, txtfShortName, txtfName, txtfType, txtfEmployeeId, txtfCity, txtfProvince, txtfCountry, txtfAddress, txtfPhone, fStatus, fStorePlace, txtfRemark)
            End If
            MsgBox(IIf(ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd, My.Resources.AddSuccess, My.resources.EditSuccess), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
            Me.DataGridView1.DataSource = mDataset.Tables(0)
            ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
            SetTextEnable(False)
            MainFrom.ToolStripAdd.Enabled = True
            MainFrom.ToolStripEdit.Enabled = True
            MainFrom.ToolStripDelete.Enabled = True
            MainFrom.ToolStripSave.Enabled = False
            MainFrom.ToolStripCancel.Enabled = False
            GetToolEnable()
            Return True
        Catch ex As Exception
            MsgBox("BtnActionSave:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Private Sub AddToDataSet(ByVal fCode As String, ByVal fShortName As String, ByVal fName As String, ByVal fType As String, ByVal fEmployeeID As String, ByVal fCity As String, _
                             ByVal fProvince As String, ByVal fCountry As String, ByVal fAddress As String, ByVal fPhone As String, ByVal fStatus As String, ByVal fStorePlace As String, ByVal fRemark As String)
        Try
            Dim mRow As DataRow = mDataset.Tables(0).NewRow
            mRow("fCode") = fCode
            mRow("fShortName") = fShortName
            mRow("fName") = fName
            mRow("fType") = fType
            mRow("fEmployeeID") = fEmployeeID
            mRow("fCity") = fCity
            mRow("fProvince") = fProvince
            mRow("fCountry") = fCountry
            mRow("fAddress") = fAddress
            mRow("fPhone") = fPhone
            If fStatus = 1 Then
                mRow("fStatus") = "已禁用"
            Else
                mRow("fStatus") = "未禁用"
            End If
            If fStorePlace = 1 Then
                mRow("fStorePlace") = "是"
            Else
                mRow("fStorePlace") = "否"
            End If
            mRow("fRemark") = fRemark
            mDataset.Tables(0).Rows.Add(mRow)
            mDataset.AcceptChanges()
        Catch ex As Exception
            MsgBox("AddToDataSet:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub EditToDataSet(ByVal fCode As String, ByVal fShortName As String, ByVal fName As String, ByVal fType As String, ByVal fEmployeeID As String, ByVal fCity As String, _
                             ByVal fProvince As String, ByVal fCountry As String, ByVal fAddress As String, ByVal fPhone As String, ByVal fStatus As String, ByVal fStorePlace As String, ByVal fRemark As String)
        Try

            Dim Rows() As DataRow = mDataset.Tables(0).Select("fCode='" & fCode & "'")
            If Rows.Length > 0 Then
                For Each mRow As DataRow In Rows
                    mRow("fShortName") = fShortName
                    mRow("fName") = fName
                    mRow("fType") = fType
                    mRow("fEmployeeID") = fEmployeeID
                    mRow("fCity") = fCity
                    mRow("fProvince") = fProvince
                    mRow("fCountry") = fCountry
                    mRow("fAddress") = fAddress
                    mRow("fPhone") = fPhone
                    If fStatus = 1 Then
                        mRow("fStatus") = "已禁用"
                    Else
                        mRow("fStatus") = "未禁用"
                    End If
                    If fStorePlace = 1 Then
                        mRow("fStorePlace") = "是"
                    Else
                        mRow("fStorePlace") = "否"
                    End If
                    mRow("fRemark") = fRemark
                Next
            End If
            mDataset.AcceptChanges()
        Catch ex As Exception
            MsgBox("EditToDataSet:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Function DeleteFromDataBase(ByVal fCode As String) As Boolean
        Try
            Dim sql As String = ""
            sql = "Delete BSC_StoreHouse where fCode='" & fCode & "';"
            sql += "delete bsc_systemprofile where Fcategory='store' and  Fkey like 'period_'+ '" & fCode & "%' "
            ReturnObject(sql)
            Return True
        Catch ex As Exception
            MsgBox("DeleteFromDataBase:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Private Function DeleteFromDataSet(ByVal fCode As String) As Boolean
        Try
            Dim Rows() As DataRow = mDataset.Tables(0).Select("fCode='" & fCode & "'")
            If Rows.Length > 0 Then
                For Each mRow As DataRow In Rows
                    mDataset.Tables(0).Rows.Remove(mRow)
                Next
            End If
            mDataset.AcceptChanges()
            Return True
        Catch ex As Exception
            MsgBox("DeleteFromDataSet:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Private Sub SetSysEnable(ByVal mTable As DataTable)
        Try
            With MainFrom
                .ToolStripFirst.Enabled = False
                .ToolStripNext.Enabled = mTable.Rows.Count > 0
                .ToolStripLast.Enabled = mTable.Rows.Count > 0
                .ToolStripPrivous.Enabled = False
            End With

        Catch ex As Exception

        End Try
    End Sub

    Private Sub GetToolEnable()
        Try
            With MainFrom
                .ToolStripAdd.Enabled = AddEnable
                .ToolStripEdit.Enabled = EditEnable
                .ToolStripDelete.Enabled = DeleteEnable
                .ToolStripPrint.Enabled = PrintEnable
                .ToolStripImport.Enabled = ImportEnable
                .ToolStripExport.Enabled = ExportEnable
                .ToolStripFind.Enabled = FindEnable
                .ToolStripFilter.Enabled = True
                .ToolStripCheck.Enabled = CheckEnable
                .ToolStripUnCheck.Enabled = UncheckEnable
                .ToolStripComplete.Enabled = CompleteEnable
                .AddToolStripMenuItem.Enabled = .ToolStripAdd.Enabled
                .Edit1ToolStripMenuItem.Enabled = .ToolStripEdit.Enabled
                .DelToolStripMenuItem.Enabled = .ToolStripDelete.Enabled
                .SaveToolStripMenuItem.Enabled = .ToolStripSave.Enabled
                .CancelToolStripMenuItem.Enabled = .ToolStripCancel.Enabled
                .PrintToolStripMenuItem.Enabled = .ToolStripPrint.Enabled
                .ExportToolStripMenuItem.Enabled = .ToolStripImport.Enabled
                .ImportToolStripMenuItem.Enabled = .ToolStripExport.Enabled
                .FilterToolStripMenuItem.Enabled = .ToolStripFilter.Enabled
            End With

        Catch ex As Exception
            MsgBox("GetToolEnable:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub


    ''' <summary>
    ''' 設置按鈕狀態
    ''' </summary>
    ''' <param name="type"></param>
    ''' <remarks></remarks>
    Private Sub SetButton(ByVal type As ButtonType, ByVal IsFind As Boolean)
        Try
            Select Case type
                Case ButtonType.Add
                    With MainFrom
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = True
                        .ToolStripCancel.Enabled = True
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = False
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = False
                        .ToolStripFilter.Enabled = False
                        .ToolStripFind.Enabled = False

                    End With

                Case ButtonType.Browse
                    With MainFrom
                        .ToolStripAdd.Enabled = True
                        .ToolStripEdit.Enabled = IsFind
                        .ToolStripDelete.Enabled = IsFind
                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = IsFind
                        .ToolStripLast.Enabled = IsFind
                        .ToolStripNext.Enabled = IsFind
                        .ToolStripPrivous.Enabled = IsFind
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

                Case ButtonType.Cancel
                    With MainFrom
                        .ToolStripAdd.Enabled = True
                        .ToolStripEdit.Enabled = IsFind
                        .ToolStripDelete.Enabled = IsFind
                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = IsFind
                        .ToolStripLast.Enabled = IsFind
                        .ToolStripNext.Enabled = IsFind
                        .ToolStripPrivous.Enabled = IsFind
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

                Case ButtonType.Edit
                    With MainFrom
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = True
                        .ToolStripCancel.Enabled = True
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = False
                        .ToolStripPrint.Enabled = False
                        .ToolStripFilter.Enabled = False
                        .ToolStripFind.Enabled = False
                    End With

            End Select
            With MainFrom
                .ToolStripCheck.Enabled = False
                .ToolStripUnCheck.Enabled = False
                .ToolStripComplete.Enabled = False
            End With
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub


#End Region

#Region "Interface Define"


    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd
        Try
            If AddEnable = False Then
                MessageBox.Show(My.Resources.NotAllowAdd, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            If Me.TabControl1.SelectedIndex = 0 Then
                Me.TabControl1.SelectedIndex = 1
            End If
            ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd
            ClearText()
            SetTextEnable(True)
            MainFrom.BtnEnable(False)
            Me.txtfCode.Focus()
            SendKeys.Send("{F4}")
        Catch ex As Exception
            MsgBox("ActionAdd:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel
        Try
            ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
            ClearText()
            SetTextEnable(False)
            If Me.TabControl1.SelectedIndex = 0 Then

            Else
                If mDataset.Tables(0).Rows.Count <= 0 Then
                    bFind = False
                Else
                    bFind = True
                    Dim fCode As String = ""
                    If Me.DataGridView1.CurrentRow Is Nothing Then
                        fCode = Me.DataGridView1.CurrentRow.Cells("fCode").Value & ""
                    End If
                    FillText(fCode)
                End If
            End If

            Me.SetButton(ButtonType.Browse, Me.DataGridView1.RowCount > 0)
        Catch ex As Exception
            MsgBox("ActionCancel:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete
        Try
            If DeleteEnable = False Then
                MessageBox.Show(My.Resources.NotAllowDelete, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            Dim fCode As String = ""
            If Not Me.DataGridView1.CurrentRow Is Nothing Then
                fCode = Me.DataGridView1.CurrentRow.Cells("fCode").Value
            End If

            If MsgBox(My.Resources.ConfirmDelete, MsgBoxStyle.Question + MsgBoxStyle.OkCancel, Msg.CompanyName) = MsgBoxResult.Ok Then
                If DeleteFromDataBase(fCode) = True AndAlso DeleteFromDataSet(fCode) = True Then
                    MsgBox(My.Resources.DeleteSuccess, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                    FillText(fCode)
                End If
            End If
            GetToolEnable()
        Catch ex As Exception
            MsgBox("ActionDelete:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit
        Try
            If EditEnable = False Then
                MessageBox.Show(My.resources.NotAllowEidt, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            If Me.TabControl1.SelectedIndex = 0 Then
                Me.TabControl1.SelectedIndex = 1
            End If
            ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit
            SetTextEnable(True)
            MainFrom.BtnEnable(False)
            Me.txtfCode.Enabled = False
            Me.txtfShortName.Focus()
            Me.txtfShortName.SelectAll()
        Catch ex As Exception
            MsgBox("ActionEdit:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport
        Try
            If App.ExportEnable = False Then
                MessageBox.Show(My.Resources.NotAllowExport, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
            SaveXls(Me.mDataset.Tables(0))
        Catch ex As Exception
            MsgBox("ActionExport:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter
        Try
            Dim Frm_Filter As New Frm_Filter
            Me.Cursor = Cursors.WaitCursor
            Frm_Filter.LoadHeaderCaption(Fields, "Frm_BSCCustomer")
            Frm_Filter.ShowDialog()
            Me.Cursor = Cursors.Default
            ReBindGridCaption(Me.DataGridView1, "Frm_BSCCustomer")
        Catch ex As Exception
            MsgBox("ActionFilter:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst
        Try
            Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(0).Cells(0)
            MainFrom.ToolStripFirst.Enabled = False
            MainFrom.ToolStripNext.Enabled = True
            MainFrom.ToolStripPrivous.Enabled = False
            MainFrom.ToolStripLast.Enabled = True
            Dim fCode As String = Me.DataGridView1.CurrentRow.Cells("fCode").Value
            If fCode.Length > 0 Then
                FillText(fCode)
            End If
        Catch ex As Exception
            MsgBox("ActionGoFirst:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast
        Try
            Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(Me.DataGridView1.RowCount - 1).Cells(0)
            MainFrom.ToolStripFirst.Enabled = True
            MainFrom.ToolStripNext.Enabled = False
            MainFrom.ToolStripPrivous.Enabled = True
            MainFrom.ToolStripLast.Enabled = False
            Dim fCode As String = Me.DataGridView1.CurrentRow.Cells("fCode").Value
            If fCode.Length > 0 Then
                FillText(fCode)
            End If
        Catch ex As Exception
            MsgBox("ActionGoLast:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext
        Try
            Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(Me.DataGridView1.CurrentRow.Index + 1).Cells(0)
            If Me.DataGridView1.CurrentRow.Index = Me.DataGridView1.RowCount - 1 Then
                MainFrom.ToolStripNext.Enabled = False
                MainFrom.ToolStripLast.Enabled = False
            Else
                MainFrom.ToolStripPrivous.Enabled = True
                MainFrom.ToolStripLast.Enabled = True
                MainFrom.ToolStripFirst.Enabled = True
                MainFrom.ToolStripNext.Enabled = True
            End If
            Dim fCode As String = Me.DataGridView1.CurrentRow.Cells("fCode").Value
            If fCode.Length > 0 Then
                FillText(fCode)
            End If
        Catch ex As Exception
            MsgBox("ActionGoNext:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious
        Try

            If Me.DataGridView1.CurrentRow Is Nothing Then Return True

            Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(Me.DataGridView1.CurrentRow.Index - 1).Cells(0)
            If Me.DataGridView1.CurrentRow.Index = 0 Then
                MainFrom.ToolStripFirst.Enabled = False
                MainFrom.ToolStripPrivous.Enabled = False
            Else
                MainFrom.ToolStripFirst.Enabled = True
                MainFrom.ToolStripNext.Enabled = True
                MainFrom.ToolStripPrivous.Enabled = True
                MainFrom.ToolStripLast.Enabled = True
            End If
            Dim fCode As String = Me.DataGridView1.CurrentRow.Cells("fCode").Value
            If fCode.Length > 0 Then
                FillText(fCode)
            End If
        Catch ex As Exception
            MsgBox("ActionGoPrevious:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport
        Try

        Catch ex As Exception
            MsgBox("ActionImport:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize
        Try
            Dim sql As String = ""
            Dim M_Ds As New DataSet
            If Me.txtfCode.Text.Length <= 0 Then
                MsgBox(My.Resources.ExistSameRecode, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Me.txtfCode.Focus()
                Return False
            End If
            If Me.txtfShortName.Text.Length <= 0 Then
                MsgBox(My.Resources.NotNullShortName, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Me.txtfShortName.Focus()
                Return False
            End If
            If Me.txtfName.Text.Length <= 0 Then
                MsgBox(My.Resources.NotNullName, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Me.txtfName.Focus()
                Return False
            End If
            If Me.ComEmployeeID.Text.Length <= 0 Then
                MsgBox(My.Resources.NotNullEmployeeID, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Me.ComEmployeeID.Focus()
                Return False
            End If
            If Me.txtfAddress.Text.Length <= 0 Then
                MsgBox(My.Resources.NotNullAddress, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Me.txtfAddress.Focus()
                Return False
            End If
            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
                sql = "select fcode from BSC_StoreHouse where fCode='" & Me.txtfCode.Text & "'"
                M_Ds = ReturnDataset(sql)
                If M_Ds.Tables(0).Rows.Count > 0 Then
                    MsgBox(My.Resources.ExistSameRecode, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                    Me.txtfCode.Focus()
                    Me.txtfCode.SelectAll()
                    Return False
                End If
            End If
            Return True
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint
        Try

        Catch ex As Exception
            MsgBox("ActionPrint:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave
        Try
            If BtnActionSave() = True Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            MsgBox("ActionSave:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect
        ActionFind()
    End Function

    Public Function ActionChecked() As Boolean
        If CheckEnable = False Then
            MessageBox.Show("對不起，您沒有審核權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Function
        End If

        Return True
    End Function

    Public Function ActionUnchecked() As Boolean
        If CheckEnable = False Then
            MessageBox.Show("對不起，您沒有反審核權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Function
        End If

        Return True
    End Function

    Public Function ActionComplete() As Boolean
        Return True
    End Function

#End Region

#Region "Control Events"

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub TabControl1_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Selected
        Try
            If Not Me.TabControl1 Is Nothing Then
                Select Case Me.TabControl1.SelectedIndex
                    Case 0
                        'MainFrom.ToolStripFind.Enabled = True
                        'MainFrom.ToolStripFilter.Enabled = True
                        'MainFrom.ToolStripImport.Enabled = True
                    Case 1
                        If mDataset.Tables(0).Rows.Count <= 0 Then
                            ClearText()
                        Else
                            Dim fCode As String = Me.DataGridView1.CurrentRow.Cells("fCode").Value
                            If fCode.Length > 0 Then
                                FillText(fCode)
                            End If
                        End If
                        'MainFrom.ToolStripFind.Enabled = False
                        'MainFrom.ToolStripFilter.Enabled = False
                        'MainFrom.ToolStripImport.Enabled = False
                End Select
            End If
        Catch ex As Exception
            MsgBox("TabControl1_Selected:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub TabControl1_Selecting(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting
        Try

            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
                e.Cancel = True
            End If
        Catch ex As Exception
            MsgBox("TabControl1_Selecting:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub BtnFind_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnFind_Q.Click
        Try
            MainFrom.ToolStripFind_Click(Nothing, Nothing)
        Catch ex As Exception
            MsgBox("BtnFind_Click:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub BtnOK_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnOK.Click
        Try
            MainFrom.ToolStripSave_Click(Nothing, Nothing)
        Catch ex As Exception
            MsgBox("BtnOK_Click1:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub BtnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Try
            MainFrom.ToolStripCancel_Click(Nothing, Nothing)
        Catch ex As Exception
            MsgBox("BtnCancel_Click:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub DataGridView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.DoubleClick
        Try
            Me.TabControl1.SelectedIndex = 1
        Catch ex As Exception
            MsgBox("DataGridView1_DoubleClick:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
        Try
            Select Case e.KeyCode
                Case Keys.Up
                    ActionGoPrevious()
                Case Keys.Down
                    ActionGoNext()
                Case Keys.Delete
                    ActionDelete()
            End Select
        Catch ex As Exception
            MsgBox("DataGridView1_KeyDown:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub DataGridView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
        Try
            If Me.DataGridView1.RowCount > 0 Then
                If Me.DataGridView1.CurrentRow.Index = 0 Then
                    MainFrom.ToolStripFirst.Enabled = False
                    MainFrom.ToolStripPrivous.Enabled = False
                    MainFrom.ToolStripNext.Enabled = True
                    MainFrom.ToolStripLast.Enabled = True
                ElseIf Me.DataGridView1.CurrentRow.Index > 0 And Me.DataGridView1.CurrentRow.Index < Me.DataGridView1.RowCount - 1 Then
                    MainFrom.ToolStripFirst.Enabled = True
                    MainFrom.ToolStripPrivous.Enabled = True
                    MainFrom.ToolStripNext.Enabled = True
                    MainFrom.ToolStripLast.Enabled = True
                ElseIf Me.DataGridView1.CurrentRow.Index = Me.DataGridView1.RowCount - 1 Then
                    MainFrom.ToolStripFirst.Enabled = True
                    MainFrom.ToolStripPrivous.Enabled = True
                    MainFrom.ToolStripNext.Enabled = False
                    MainFrom.ToolStripLast.Enabled = False
                End If
            End If


        Catch ex As Exception
            MsgBox("DataGridView1_MouseDown:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub fCode_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles fCode.KeyDown, fCity.KeyDown, fCountry.KeyDown, fShortName.KeyDown, fStatus.KeyDown, fType.KeyDown
        Try
            If e.KeyCode = Keys.Shift Then
                e.Handled = True
                If e.KeyCode = Keys.Enter Then
                    SendKeys.Send("+{TAB}")
                    SendKeys.Send("{F4}")
                End If
            Else
                Select Case e.KeyCode
                    Case Keys.Enter
                        e.Handled = True
                        SendKeys.Send("{TAB}")
                        SendKeys.Send("{F4}")
                End Select
            End If
        Catch ex As Exception
            MsgBox("fCode_KeyDown:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtfCode_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtfAddress.KeyDown, txtfCity.KeyDown, txtfCountry.KeyDown, txtfName.KeyDown, txtfProvince.KeyDown, txtfRemark.KeyDown, txtfStatus.KeyDown, txtfType.KeyDown, txtfCode.KeyDown, txtfShortName.KeyDown
        Try
            If e.KeyCode = Keys.Shift Then
                e.Handled = True
                If e.KeyCode = Keys.Enter Then
                    SendKeys.Send("+{TAB}")
                    SendKeys.Send("{F4}")
                End If
            Else
                Select Case e.KeyCode
                    Case Keys.Enter
                        e.Handled = True
                        SendKeys.Send("{TAB}")
                        SendKeys.Send("{F4}")
                End Select
            End If
        Catch ex As Exception
            MsgBox("txtfCode_KeyDown:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub


    Private Sub txtfCode_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtfCode.SelectedValueChanged
        Try
            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
                If Not Me.txtfCode.SelectedValue.ToString Is Nothing Then
                    Me.txtfShortName.Text = Me.txtfCode.SelectedValue.ToString
                End If
            End If
        Catch ex As Exception
            MsgBox("txtfCode_SelectedValueChanged:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

#End Region


End Class
